<?php
class Admin_AccountController extends ZF_AdminController{
    public function indexAction(){
        $model = new Admin_Model_SystemUser();
        $account =  $model->listAll();
        
        $this->view->accounts = $account;
        
        $this->view->title = "System Account Management";
    }
    public function addAction(){
        $form = new Admin_Form_AddAccountForm();
        //print_r($form->);
        if ($this->getRequest()->isPost()){
            if ($form->isValid($_POST)){
                $model = new Admin_Model_SystemUser();
                $result = $model->insert($form->getValue("username"), 
                        $form->getValue("password"), 
                        $form->getValue("email"));
                if ($result>0){
                    $this->_redirect($this->baseUrl."/".$this->module."/account/index");
                }
            }
        }
        $form->setAction($this->baseUrl."/".$this->module."/account/add");
        $this->view->form = $form;
        $this->view->title="Add new Account";
        
    }
    public function editAction(){
        $form = new Admin_Form_EditAccountForm();
    	$model = new Admin_Model_SystemUser();
		if ($this->getRequest()->isPost()){
			if ($form->isValid($_POST)){
					$model->getById($form->getValue("id"));
					//$model->setUsername($form->getValue("username"));
					$password = $form->getValue('password');
					$repassword = $form->getValue("repassword");
					if($password == $repassword ){
						if($password != "")
						{
							$model->setPassword($password);
							$model->setEmail($form->getValue('email'));
							$result = $model->update();
							if($result>0)
								$this->_redirect($this->baseUrl."/".$this->module."/account/index");
							else{
								
							}
						}
						else{
							
						}
						
				}
				else{					
				}
			}
		}
		else{
			$id = $this->getRequest()->getParam('id',null);						
			$data = $model->getById($id);					
			$form->populate($data);
		}
                $form->setAction($this->baseUrl."/".$this->module."/account/edit");
                $this->view->form = $form;
    }
    public function delAction(){        
        //$this->_helper->viewRenderer->setNoRender(true);
         $id = $this->getRequest()->getParam('id',null);
         $model = new Admin_Model_SystemUser();
         $model->getById($id);
	 $result = $model->delete();
	 if($result > 0)
	 	$this->_redirect($this->baseUrl."/".$this->module."/account/index");
	 else 
		 $this->view->error = "Không thể xóa !!!";
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
